# -*- coding: utf-8 -*-
'''
Created on 20/set/2015

@author: Daniel
'''
import subprocess
import os
import sys
import webbrowser
import logging
from utils         import utils 

class WrapperWindows(object):
    
    def __init__(self):
        super(WrapperWindows,self).__init__()
        
#     def computePath(self, path):
#         outPath = ''
#         targetCharachter = '/'
#         elementsList = path.split(targetCharachter)
#         if len(elementsList) == 1:
#             targetCharachter = '\\'
#             elementsList = path.split(targetCharachter)
#         for element in elementsList:
#             if ' ' in element:
#                 element = '"%s"'%(element)
#             outPath = outPath+element+targetCharachter
#         outPath = outPath.replace('/', '\\')
#         if outPath.startswith('\\\\'):
#             outPath + outPath[1:]
#         return outPath[:-1]
    
    def launchProgramAndFeatures(self):
        try:
            subprocess.call("start appwiz.cpl",shell=True)
        except Exception,ex:
            utils.launchMessage(unicode(ex))

    def launchExe(self, exePath, arguments=[]):
        logging.info('[launchExe] exePath: %s, arguments: %s' % (exePath, arguments))
        try:
            if not arguments:
                self.openCommon(exePath)
            else:
                # May in case of links is not necessary to open with "start" word
                stringToOpen = '"%s"' % (exePath).encode(sys.getfilesystemencoding())
                
                for arg in arguments:
                    stringToOpen = stringToOpen + ' %s' % (arg)
                logging.info('[launchExe 2] exePath: %s, arguments: %s' % (exePath, arguments))
                subprocess.call(stringToOpen, shell=True)
        except Exception, _ex:
            utils.logMessage('error', unicode(_ex), 'launchExe')
            
    def launchSchreenshotOS(self,):
        try:
            strrr = 'C:\Windows\explorer.exe C:\Windows\system32\SnippingTool.exe'
            subprocess.call(strrr, shell=True)
        except Exception, ex:
            utils.launchMessage(unicode(ex))
            
    def launchTextEditor(self):
        try:
            subprocess.call("start notepad",shell=True)
        except Exception,ex:
            utils.launchMessage(unicode(ex))
            
    def openFolder(self, folderPath):
        logging.debug('[openFolder] folderPath: %s' % (folderPath))
        try: 
            if os.path.exists(folderPath):
                self.openCommon(folderPath)
        except Exception,ex:
            utils.launchMessage(unicode(ex))
            
    def openCommon(self, path):
        toOpen = '"%s"' % (path).encode(sys.getfilesystemencoding())
        logging.debug('[openCommon] toOpen: %s' % (toOpen))
        os.startfile(toOpen)

    def openBrowser(self, browserPath1, openSites=[]):
        logging.info('[openBrowser] browserPath1: %s, openSites: %s' % (browserPath1, openSites))
        try:
            if not openSites:
                return self.launchExe(browserPath1)
            first = True
            try:
                if browserPath1:
                    self.launchExe(browserPath1, openSites)
                    return 
            except Exception, ex:
                logging.warning(ex)
            for siteName in openSites:
                if first:
                    webbrowser.open_new(siteName.encode(sys.getfilesystemencoding()))
                else:
                    webbrowser.open_new_tab(siteName.encode(sys.getfilesystemencoding()))
        except Exception,ex:
            utils.launchMessage(unicode(ex))
            
    def launchCmd(self, toExec):
        logging.info('[launchCmd] toExec: %s' % (toExec))
        if toExec:
            try:
                toExec = 'start cmd /k ' + toExec
                subprocess.call(toExec, shell=True)
            except Exception, ex:
                utils.launchMessage(unicode(ex))
        